//
//  RecommendViewController.m
//  HimalayaMusic
//
//  Created by dllo on 16/1/6.
//  Copyright © 2016年 WXCLegendary. All rights reserved.
//

#import "RecommendViewController.h"

@interface RecommendViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout,RecommendCollectionViewCellDelegate, ListDelegate, CellDelegate, DetailDelegate, BroadcastCollectionViewCellDelegate>

@property (nonatomic, strong)NSMutableArray *columnArray;
@property (nonatomic, strong)ColumnView *columnView;
@property (nonatomic, strong)UICollectionView *collectionView;
@property (nonatomic, strong)NSString *categoryID;
@property (nonatomic,strong)MBProgressHUD *hud;
@end

@implementation RecommendViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"发现";
    [self getColumnData];
    [self creatCollectionView];
    [self creatColumnView];
    
    self.hud = [MBProgressHUD showHUDAddedTo:self.collectionView animated:YES];
    self.hud.color = [UIColor blackColor];
    self.hud.dimBackground = YES;
    self.hud.labelText = @"正在加载";
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pushAction:) name:@"push" object:self.categoryID];
    
    //初始化搜索按钮
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(searchAction:)];
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor redColor];
    
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"album"] style:UIBarButtonItemStyleDone target:self action:@selector(leftAction)];
    
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor redColor];
    
    
    
    
}

- (void)leftAction {
    PlayViewController *playVC = [PlayViewController sharePlayViewController];
    [self presentViewController:playVC animated:YES completion:nil];
    
    
}

- (void)searchAction:(UIBarButtonItem *)sender {
    SearchViewController *searchVC = [[SearchViewController alloc] init];
    [self.navigationController pushViewController:searchVC animated:YES];
}



//发送通知

- (void)pushAction:(NSNotification *)center {

    MoreViewController *moreVC = [[MoreViewController alloc] init];
    moreVC.categoryId = center.object;
    
    [self.navigationController pushViewController:moreVC animated:YES];
    
}

- (void)passValue:(HotRecommendListModel *)hotRecommendListModel {
    AlbumViewController *albumVC = [[AlbumViewController alloc] init];
    albumVC.hotRecommendListModel = hotRecommendListModel;
    [self.navigationController pushViewController:albumVC animated:YES];
}



- (void)getColumnData {
    self.columnArray = [NSMutableArray array];
    [WXCNetWorkTool getWithUrl:FoundColumnURL parameter:nil httpHeader:nil responseType:ResponseTypeJson success:^(id result) {
        
        [self.columnArray addObjectsFromArray:[[result objectForKey:@"tabs"] objectForKey:@"list"]];

        [self.collectionView reloadData];
        [self.hud hide:YES];
    } fail:^(NSError *error) {
        
    }];
    
}

- (void)creatColumnView {
    self.columnView = [[ColumnView alloc] initWithFrame:CGRectMake(0, 64, self.view.width, 40)];
    self.columnView.backgroundColor = [UIColor colorWithRed:206 / 255.0 green:71 / 255.0 blue:56 / 255.0 alpha:1];
    
    
    self.columnView.collectionView = self.collectionView;
    
    [self.view addSubview:self.columnView];
  
//    for (int i = 0; i < 5; i++) {
//        UIButton *button = (UIButton *)[self.columnView viewWithTag:100 + i];
//        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
//    }
}

- (void)buttonAction:(UIButton *)sender {
//    self.collectionView.contentOffset = CGPointMake((sender.tag - 100) * self.view.width, 0);
    for (UIButton *button in self.columnView.buttonArray) {
        if (button.tag == sender.tag) {
            [sender setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            sender.titleLabel.font = [UIFont systemFontOfSize:18];
        } else {
            [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            button.titleLabel.font = [UIFont systemFontOfSize:15];
        }
    }
}



- (void)creatCollectionView {
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    layout.itemSize = CGSizeMake(self.view.width, self.view.height - 40 - 64 - 49);
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 104, self.view.width, self.view.height - 40 - 64 - 49) collectionViewLayout:layout];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.pagingEnabled = YES;
    self.collectionView.bounces = NO;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    [self.collectionView registerClass:[RecommendCollectionViewCell class] forCellWithReuseIdentifier:@"RecommendCollectionViewCellIdentifier"];
    [self.collectionView registerClass:[ListCollectionViewCell class] forCellWithReuseIdentifier:@"ListCollectionViewCell"];
    [self.collectionView registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:@"CollectionViewCell"];
    [self.collectionView registerClass:[ClassifyCollectionViewCell class] forCellWithReuseIdentifier:@"ClassifyCollectionViewCell"];
    [self.collectionView registerClass:[BroadcastCollectionViewCell class] forCellWithReuseIdentifier:@"BroadcastCollectionViewCell"];

    [self.view addSubview:self.collectionView];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 4;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.item == 0) {
        RecommendCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"RecommendCollectionViewCellIdentifier" forIndexPath:indexPath];
        cell.delegate = self;
        return cell;
    } else if (indexPath.item == 1) {
        ClassifyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ClassifyCollectionViewCell" forIndexPath:indexPath];
        cell.newBlock = ^(NSString *string) {
            MoreViewController *moreVC = [[MoreViewController alloc] init];
            moreVC.categoryId = string;
            [self.navigationController pushViewController:moreVC animated:YES];
            
            
            
            
            
        };
        return cell;
    }
    else if (indexPath.item == 2) {
        ListCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ListCollectionViewCell" forIndexPath:indexPath];
        cell.delegate = self;
        return cell;
    } else {
        CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CollectionViewCell" forIndexPath:indexPath];
        cell.delegate = self;
        cell.delegates = self;
        return cell;
    }
    
    
//    return cell;
    
    
    
    
    
    
    
    
    
    
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    
    
}

//whs协议方法
- (void)changeVC:(NSString *)url withcontentType:(NSString *)contentType {
    ListDetailViewController *listVC = [[ListDetailViewController alloc] init];
    listVC.url = url;
    listVC.contentType = contentType;
    [self.navigationController pushViewController:listVC animated:YES];
}

-(void)changeAndGet:(NSString *)name withCondition:(NSNumber *)condition {
    MuchViewController *viewController = [[MuchViewController alloc] init];
    viewController.name = name;
    //    viewController.condition = condition.stringValue;
    
    [self.navigationController pushViewController:viewController animated:YES];
}

-(void)changeVC:(NSNumber *)number withUrl:(NSString *)url withTitle:(NSString *)title {
    AnchorDetailViewController *detail = [[AnchorDetailViewController alloc] init];
    detail.uid = number;
    detail.url = url;
    detail.title = title;
    
    [self.navigationController pushViewController:detail animated:YES];
}

//YB代理
- (void)passPage {
    PlayViewController *playVC = [PlayViewController sharePlayViewController];
    
    playVC.url = @"http://fdfs.xmcdn.com/group7/M00/E4/5C/wKgDX1aUsk_Ce426AJuD7jRMRLU844.mp3";
    [self presentViewController:playVC animated:YES completion:^{
    }];
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

    
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
